﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JoinBoxCurrency
{
    public static class EnumerableUp
    {
        /// <summary>
        /// 判断集合是否为空
        /// </summary> 
        public static bool IsNull<TSource>(this IEnumerable<TSource> source)
        {
            return !source.IsNullNo();
        }
        public static bool IsNullNo<TSource>(this IEnumerable<TSource> source)
        {
            if (source == null || source.Count() == 0)
            {
                return false;
            }
            // source.GetEnumerator().MoveNext();
            return true;
        }
        /// <summary>
        /// 判断数组是否为空
        /// </summary> 
        public static bool IsNull<TSource>(this TSource[] source)
        {
            return !source.IsNoNull();
        }
        public static bool IsNoNull<TSource>(this TSource[] source)
        {
            if (source == null)
            {
                return false;
            }
            if (source.GetEnumerator().MoveNext())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 仿lisp的mapcar函数
        /// </summary>
        /// <typeparam name="T1">集合</typeparam>
        /// <typeparam name="T2">集合</typeparam>
        /// <typeparam name="TR"></typeparam>
        /// <param name="lst1"></param>
        /// <param name="lst2"></param>
        /// <param name="func"></param>
        /// <returns></returns>     
        //List<double> 1st1 = new List<double>{1，2，3, 4} :
        //List<double> 1st2 = new List<double> { 1, 2, 3, 4 } :
        //var 1st3 = Mapcar(1st1, 1st2, (a, b) => a + b) : 
        public static IEnumerable<TR> Mapcar<T1, T2, TR>(IEnumerable<T1> lst1, IEnumerable<T2> lst2, Func<T1, T2, TR> func)
        {
            var itor1 = lst1.GetEnumerator();
            var itor2 = lst2.GetEnumerator();
            while (itor1.MoveNext() && itor2.MoveNext())
                yield return func(itor1.Current, itor2.Current);
        }

        //https://www.jianshu.com/p/476b112fd77a
        //和下面等价
        //public IEnumerable<int> Method()
        //{
        //    List<int> results = new List<int>();
        //    int counter = 0;
        //    int result = 1;
        //    while (counter++ < 10)
        //    {
        //        result = result * 2;
        //        results.Add(result);
        //    }
        //    return results;
        //} 
        //public IEnumerable<int> YieldDemo()
        //{
        //    int counter = 0;
        //    int result = 1;
        //    while (counter++ < 10)
        //    {
        //        result *= 2;
        //        yield return result;
        //    }
        //}
    }
}
